package com.globallogic.week1xml;

import generated.Catalog;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;

public class XMLHandler {
	
	private static final Logger log = Logger.getLogger(XMLHandler.class); 
	
	
	
	public String marshalIntoString(Catalog catalog){
		String result = "";
		log.info("Start to marshall object into XML-String...");
		
		try {
			JAXBContext context = JAXBContext.newInstance(Catalog.class);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

			StringWriter sw = new StringWriter();
			StreamResult out = new StreamResult(sw);
			
			marshaller.marshal(catalog, out);
			result = ((StringWriter)out.getWriter()).getBuffer().toString();
		} catch (JAXBException e) {
			e.printStackTrace();
			log.error("Error wile marshaling into String!");
			log.error(e.getStackTrace());
		}
		log.info("Catalog marshaled into XML-String successfully.");
		return result;
	}
	
	public void marshalIntoXMLFile(Catalog catalog, String path){
		String result = "";
		log.info("Start to marshall object into XML file...");
		
		try {
			JAXBContext context = JAXBContext.newInstance(Catalog.class);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			marshaller.marshal(catalog, new File(path));
			log.info("Catalog marshaled into XML file successfully.");
			
		} catch (JAXBException e) {
			e.printStackTrace();
			log.error("Error wile marshaling into XML file!");
			log.error(e.getStackTrace());
		}
	}
	
	public Catalog unmarshal(String xmlString){
		log.info("Begin to unmarshall object from XML file.");
		Catalog catalog = null;
		InputStream is = new ByteArrayInputStream(xmlString.getBytes()); 
		try {
			JAXBContext context = JAXBContext.newInstance(Catalog.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			catalog = (Catalog)unmarshaller.unmarshal(is); 
		} catch (JAXBException e) {
			e.printStackTrace();
			log.error(e.getStackTrace());
		}
		log.info("XML unmarshaled into Object successfully");
		return catalog;
	}
	
	
	public Catalog unmarshal(File file){
		log.info("Begin to unmarshall object from XML file.");
		Catalog catalog = null;
		
		try {
			JAXBContext context = JAXBContext.newInstance(Catalog.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			catalog = (Catalog)unmarshaller.unmarshal(file); 
		} catch (JAXBException e) {
			e.printStackTrace();
			log.error(e.getStackTrace());
		}
		
		log.info("XML unmarshaled into Object successfully");
		return catalog;
	}
}
